﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using FunDraw.LayerInfos;

namespace FunDraw
{
    [ToolboxBitmap(typeof(MyDrawLayer))]
    public partial class MyDrawLayer : Panel
    {
        private Color _borderColor;
        private int _borderWidth = 0;
        private DashStyle _borderStyle = DashStyle.Solid;
        private int _opacity = 125;

        private event MouseEventHandler _LayerMouseMove;
        public event MouseEventHandler LayerMouseMove
        {
            add
            {
                _LayerMouseMove += value;
            }
            remove
            {
                _LayerMouseMove -= value;
            }
        }
        
        public virtual void OnLayerMouseMove(object sender, MouseEventArgs e)
        {
            _LayerMouseMove.Invoke(sender, e);
        }
        /// <summary>
        /// 方程类接口
        /// </summary>
        public IntEquation equation { get; set; } = null;

        public MyDrawLayer()
        {

        }
       

        #region Property
        [Category("Custom"), Description("Border Color")]
        public Color BorderColor
        {
            set { _borderColor = value; }
            get { return _borderColor; }
        }

        [Category("Custom"), Description("Border Width"), DefaultValue(1)]
        public int BorderWidth
        {
            set
            {
                if (value < 0) value = 0;
                _borderWidth = value;
            }
            get { return _borderWidth; }
        }

        [Category("Custom"), Description("Border Style"), DefaultValue(DashStyle.Solid)]
        public DashStyle BS
        {
            set { this._borderStyle = value; this.Invalidate(); }
            get { return this._borderStyle; }
        }

        [Bindable(true), Category("Custom"), DefaultValue(125), Description("背景的透明度. 有效值0-255")]
        public int Opacity
        {
            get { return _opacity; }
            set
            {
                if (value > 255) value = 255;
                else if (value < 0) value = 0;
                _opacity = value;
                this.Invalidate();
            }
        }

        #endregion
        
        protected override void OnPaintBackground(PaintEventArgs e)
        {
           
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this._opacity, this.BackColor)),
                                         this.ClientRectangle);

        }
        
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }
        /// <summary>
        /// 绘制图层
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {

            if (this._opacity > 0)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this._opacity, this.BackColor)),
                                         this.ClientRectangle);
            }
            if (this._borderWidth > 0)
            {
                Pen pen = new Pen(this._borderColor, this._borderWidth);
                pen.DashStyle = this._borderStyle;
                e.Graphics.DrawRectangle(pen, e.ClipRectangle.Left, e.ClipRectangle.Top, this.Width - 1, this.Height - 1);
                pen.Dispose();
            }
            if (!(equation is null))
            {
                Graphics g = e.Graphics;
                equation.DrawGraph(g);
            }   
                
        }

        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyDrawLayer_MouseMove(object sender, MouseEventArgs e)
        {
            OnLayerMouseMove(sender, e);
        }
    }
}

